package com.xiaoq.order.repository.impl;

import com.xiaoq.order.entity.ShopOrderCardEntity;
import com.xiaoq.order.repository.IShopOrderCardRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by chenjh on 2018/5/22.
 */
@Repository
public class ShopOrderCardRepositoryImpl implements IShopOrderCardRepository{

    private static final Logger LOGGER = LoggerFactory.getLogger(ShopOrderCardRepositoryImpl.class);

    @Autowired
    private Datastore datastore;

    @Override
    public void save(ShopOrderCardEntity shopOrderCardEntity) {
        if(shopOrderCardEntity != null && StringUtils.isNotBlank(shopOrderCardEntity.getId())){
            datastore.save(shopOrderCardEntity);
        }
    }

    @Override
    public List<ShopOrderCardEntity> getListByOrderIdList(String shopId, List<String> shopOrderIdList, Long minCreatedTime, Long maxCreatedTime) {
        Query<ShopOrderCardEntity> shopOrderCardQuery = datastore.createQuery(ShopOrderCardEntity.class);
        ShopOrderCardEntity orderCardEntity = new ShopOrderCardEntity();
        if(StringUtils.isNotBlank(shopId)){
            shopOrderCardQuery.filter("shopId", shopId);
        }
        if (CollectionUtils.isNotEmpty(shopOrderIdList)){
            shopOrderCardQuery.filter("shopOrderId in", shopOrderIdList);
        }
        if(minCreatedTime != null){
            shopOrderCardQuery.filter("createdAt >=", minCreatedTime);
        }
        if (maxCreatedTime != null){
            shopOrderCardQuery.filter("createdAt <=", maxCreatedTime);
        }
        return shopOrderCardQuery.asList();
    }


}
